package com.jaya.budan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.CountDownTimer;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class Forget_Password_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back, rl_forget_password_mobile;
    private EditText et_forget_mobile, et_forget_yzm, et_forget_password, et_forget_rpassword;
    private Button btn_forget_getSMS, btn_forget_ok;
    CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            btn_forget_getSMS.setText(millisUntilFinished / 1000 + "秒后重试");
            btn_forget_getSMS.setEnabled(false);
        }

        @Override
        public void onFinish() {
            btn_forget_getSMS.setText("获取验证码");
            btn_forget_getSMS.setEnabled(true);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        back = findViewById(R.id.back);
        rl_forget_password_mobile = findViewById(R.id.rl_forget_password_mobile);
        et_forget_mobile = findViewById(R.id.et_forget_mobile);
        et_forget_yzm = findViewById(R.id.et_forget_yzm);
        et_forget_password = findViewById(R.id.et_forget_password);
        et_forget_rpassword = findViewById(R.id.et_forget_rpassword);
        btn_forget_getSMS = findViewById(R.id.btn_forget_getSMS);
        btn_forget_ok = findViewById(R.id.btn_forget_ok);
        back.setOnClickListener(this);
        rl_forget_password_mobile.setOnClickListener(this);
        btn_forget_getSMS.setOnClickListener(this);
        btn_forget_ok.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.rl_forget_password_mobile:
                ToolUtils.showSoftInputFromWindow(et_forget_mobile);
                break;
            case R.id.btn_forget_getSMS:
                JSONObject object = new JSONObject();
                try {
                    object.put("username", et_forget_mobile.getText().toString().trim());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                postData_sms(object);//获取验证码
                break;
            case R.id.btn_forget_ok:
                JSONObject object2 = new JSONObject();
                try {
                    object2.put("username", et_forget_mobile.getText().toString().trim());
                    object2.put("sms", et_forget_yzm.getText().toString().trim());
                    object2.put("password", et_forget_password.getText().toString().trim());
                    object2.put("r_password", et_forget_rpassword.getText().toString().trim());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                postData(object2);//重置密码
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     * @param object
     */
    private void postData(JSONObject object) {
        Log.d("forget", object.toString());
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.reset_Password_Url, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("forget", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        JSONObject info = jsonObject.getJSONObject("info");
                        String access_token = info.getString("access_token");
                        ToolUtils.showToast(Forget_Password_Activity.this, message);
                        Intent intent = new Intent(Forget_Password_Activity.this, Reset_Password_Activity.class);//已重置密码，返回登录页
                        intent.putExtra("", access_token);
                        startActivity(intent);
                        finish();
                    } else {
                        ToolUtils.showToast(Forget_Password_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Forget_Password_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     * @param object
     */
    private void postData_sms(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.register_GetSMS_Url, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("forget_sms", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
//                        JSONObject info = jsonObject.getJSONObject("info");
//                        data = info.getInt("data");
                        timer.start();//启用倒计时
                        ToolUtils.showToast(Forget_Password_Activity.this, message);
                    } else {
                        ToolUtils.showToast(Forget_Password_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Forget_Password_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
